<template>
  <div class="main-con flex1 vbox">
    <div class="hbox" style="padding-top:10px">
      <el-form :model="searchParam" style="width:600px" label-width="110px">
        <el-col :span="12">
          <el-form-item label="产品">
            <el-select
              style="width:100%"
              clearable
              filterable
              v-model="searchParam.prdCode"
              placeholder="请选择产品"
            >
              <el-option
                v-for="item in products"
                :key="item.id"
                :label="item.prdName"
                :value="item.prdCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="代理商">
            <el-select
              style="width:100%"
              clearable
              filterable
              v-model="searchParam.dlsMngId"
              placeholder="请选择代理商"
            >
              <el-option v-for="item in dls" :key="item.id" :label="item.username" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设置折扣类型">
            <base-select
              v-model.trim="searchParam.discountType"
              dictCode="discount_type"
              placeholder="请选择折扣类型"
            ></base-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="总价折扣">
            <el-input-number
              :disabled="searchParam.discountType != 1"
              style="width:100%"
              :min="0"
              :precision="3"
              v-model="searchParam.totalDiscount"
              placeholder="请输入总价折扣"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>
      <div style="padding-left:30px;">
        <el-button :disabled="!searchParam.discountType" @click="saveSet">保存设置</el-button>
      </div>
    </div>
    <div class="vbox flex1" style="margin-top:20px;">
      <el-table :data="productDiscounts" height="100%" style="width: 100%">
        <el-table-column label="路向" prop="destination" width="80"></el-table-column>
        <el-table-column label="国家代码" prop="countryCode" width="80"></el-table-column>
        <el-table-column label="首重价格" prop="startingPrice" width="80"></el-table-column>
        <el-table-column label="续重价格" prop="ratePrice" width="80"></el-table-column>
        <el-table-column label="总价折扣率">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.discount"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="折扣后首重价格">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.price"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="折扣后续重价格">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.disRatePrice"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="税">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.tax"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="异常">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.abnormal"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="改单">
          <template slot-scope="scope">
            <el-input-number :min="0" :precision="3" v-model="scope.row.changeOrder"></el-input-number>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchParam: {
        prdCode: "",
        dlsMngId: "",
        discountType: "",
        totalDiscount: ""
      },
      products: [],
      dls: [],
      priceList: [],
      productDiscounts: []
    };
  },
  watch: {
    "searchParam.prdCode"(v) {
      this.getPrices(v);
    },
    "searchParam.dlsMngId"(v) {
      this.getDlss();
    },
    "searchParam.discountType"(v) {
      let me = this;
      if (v == 2) {
        this.searchParam.totalDiscount = 0;
        this.productDiscounts.forEach(price => {
          price.discount = 0;
        });
      } else if (v == 1) {
        this.productDiscounts.forEach(price => {
          price.discount = me.searchParam.totalDiscount;
        });
      }
    },
    "searchParam.totalDiscount"(v) {
      if (this.searchParam.discountType == 1) {
        this.productDiscounts.forEach(price => {
          price.discount = v;
        });
      }
    }
  },
  created() {
    this.getAdmins();
    this.getProducts();
  },
  methods: {
    getProducts() {
      let me = this;
      me.$http.ajax({
        url: "product/getAll",
        code: "htk",
        success: res => {
          me.products = res;
        }
      });
    },
    getAdmins() {
      let me = this;
      me.$http.ajax({
        url: "admin/getAdminByType/2",
        success: res => {
          me.dls = res;
        }
      });
    },
    getPrices(prdCode) {
      let me = this;
      me.$http.ajax({
        url: "price/listByPrdCode/" + prdCode,
        code: "htk",
        success: res => {
          me.priceList = res;
          me.productDiscounts = [];
          me.priceList.forEach(item => {
            me.productDiscounts.push({
              priceId: item.id,
              destination: item.destination,
              countryCode: item.countryCode,
              startingPrice: item.startingPrice,
              ratePrice: item.ratePrice
            });
          });
          me.$nextTick(() => {
            me.getDlss(prdCode);
          });
        }
      });
    },
    getDlss() {
      let me = this;
      if (!me.searchParam.prdCode || !me.searchParam.dlsMngId) {
        return;
      }
      me.$http.ajax({
        url:
          "discount/listDiscount/" +
          me.searchParam.prdCode +
          "/" +
          me.searchParam.dlsMngId,
        code: "htk",
        success: res => {
          if (res) {
            if (res.length > 0) {
              res.forEach(item => {
                me.productDiscounts.forEach(price => {
                  if (item.priceId == price.priceId) {
                    price.discount = item.discount;
                    price.price = item.price;
                    price.disRatePrice = item.disRatePrice;
                    price.tax = item.tax;
                    price.abnormal = item.abnormal;
                    price.changeOrder = item.changeOrder;
                    price.id = item.id;
                    price.mngId = me.searchParam.dlsMngId;
                  }
                });
              });
            } else {
              me.productDiscounts.forEach(price => {
                price.discount = "";
                price.price = "";
                price.disRatePrice = "";
                price.tax = "";
                price.abnormal = "";
                price.changeOrder = "";
                price.id = "";
                price.mngId = "";
              });
            }
          }
        }
      });
    },
    saveSet() {
      let me = this;
      me.productDiscounts.forEach(item => {
        item.mngId = me.searchParam.dlsMngId;
      });
      me.$http.ajax({
        url: "discount/saveOrUpdate",
        code: "htk",
        data: {
          list: me.productDiscounts,
          crteMngId: me.$store.state.admin.id
        },
        success: res => {
          me.$message.success("保存成功");
        }
      });
    }
  }
};
</script>

<style>
</style>
